﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BaHuan.Common;
using BaHuan.BusinessLayer;
using BaHuan.Entities;
using System.Data.Linq.SqlClient;
using System.Collections;

namespace BaHuan
{
    public partial class FrmBCDKT : Form
    {
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private KhachHangManagement khachhangMgt = new KhachHangManagement();
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();        
        private NhaCungCapManagement nhacungcapMgt = new NhaCungCapManagement();
        private BangCanDoiKeToanManagement bangcandoiketoanMgt = new BangCanDoiKeToanManagement();
        private CTTKKeToanLuongTinhManagement cttkketoanluongtinhMgt = new CTTKKeToanLuongTinhManagement();
        
        private ChiTieuNgoaiBangCanDoiKeToanManagement chitieungoaibangcandoiketoanMgt = new ChiTieuNgoaiBangCanDoiKeToanManagement();

        private string[] arrTKLuongTinh = { "131", "331" }; // List of hybrid account

        public FrmBCDKT()
        {
            InitializeComponent();
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {            
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;

            List<BangCanDoiKeToan> lstBCDKT = GetAllBangCanDoiKeToan();
            
            foreach (BangCanDoiKeToan r in lstBCDKT)
            {
                if (!string.IsNullOrEmpty(r.TKNo) && string.IsNullOrEmpty(r.TKCo))
                {
                    string[] arrTKNo = r.TKNo.Split(',');
                    r.SoDauKy = SoDuTKThuong(arrTKNo, dtTuNgay.AddDays(-1), 0);
                    r.SoCuoiKy = SoDuTKThuong(arrTKNo, dtDenNgay, 0);
                }
                else if (string.IsNullOrEmpty(r.TKNo) && !string.IsNullOrEmpty(r.TKCo))
                {
                    string[] arrTKCo = r.TKCo.Split(',');
                    r.SoDauKy = SoDuTKThuong(arrTKCo, dtTuNgay.AddDays(-1), 1);
                    r.SoCuoiKy = SoDuTKThuong(arrTKCo, dtDenNgay, 1);
                }
            }

            int i = 3;
            while (i >= 0)
            {
                lstBCDKT = UpdateAmount(lstBCDKT, i);
                i--;
            }

            FrmXemReport frm = new FrmXemReport(lstBCDKT);
            this.Hide();
            frm.ShowDialog();
            this.Show();
        }

        public List<BangCanDoiKeToan> UpdateAmount(List<BangCanDoiKeToan> lstBCDKT, int level)
        {
            var lst = (from l in lstBCDKT                       
                       select l);

            if (level == 3)
                lst = lst.Where(p => p.IsThirdSubTitle == 1);
            else if (level == 2)
                lst = lst.Where(p => p.IsSecondSubTitle == 1);
            else if (level == 1)
                lst = lst.Where(p => p.IsFirstSubTitle == 1);
            else if (level == 0)
                lst = lst.Where(p => p.IsMainTitle == 1 && p.Parent != 0);

            foreach (var l in lst)
            {
                int index = lstBCDKT.FindIndex(p => p.Id == l.Parent);
                lstBCDKT[index].SoDauKy += l.SoDauKy;
                lstBCDKT[index].SoCuoiKy += l.SoCuoiKy;
            }

            return lstBCDKT;
        }

        public long SoDuTKThuong(string[] arr, DateTime dt, int type)
        {
            // Type 0: TK No
            // Type 1: TK Co

            long lReturnValue = 0;
            for (int i = 0; i < arr.Length; i++)
            {
                if (arrTKLuongTinh.Any(s => s.StartsWith(arr[i])))
                {
                    lReturnValue += SoDuTKLuongTinh(arr[i], dt, type);
                }
                else
                {
                    Dictionary<string, long> dictionaryButToan = new Dictionary<string, long>();

                    List<ButToan> lst = (from x in GetAllButToan()
                                         where x.NgayThang.Date <= dt.Date && x.TKGoc.StartsWith(arr[i])
                                         select x).ToList();

                    if (lst.Count > 0)
                    {
                        foreach (ButToan bt in lst)
                        {
                            if (!dictionaryButToan.ContainsKey(bt.TKGoc))
                            {
                                dictionaryButToan.Add(bt.TKGoc, 0);

                                TaiKhoanKeToan tkkt = (from a in GetAllTaiKhoanKeToan()
                                                       where a.MaSoTK == bt.TKGoc
                                                       select a).FirstOrDefault();

                                if (tkkt != null)
                                {
                                    if (type == 0)
                                        dictionaryButToan[bt.TKGoc] = (tkkt.SoDuBanDau + bt.GhiNo) - bt.GhiCo;
                                    else                                    
                                        dictionaryButToan[bt.TKGoc] = (tkkt.SoDuBanDau + bt.GhiCo) - bt.GhiNo;
                                }
                            }
                            else
                            {
                                long lSoDu = dictionaryButToan[bt.TKGoc];
                                if (type == 0)
                                    dictionaryButToan[bt.TKGoc] = (lSoDu + bt.GhiNo) - bt.GhiCo;
                                else
                                    dictionaryButToan[bt.TKGoc] = (lSoDu + bt.GhiCo) - bt.GhiNo;
                            }
                        }
                    }

                    foreach (var pair in dictionaryButToan)
                    {
                        lReturnValue += pair.Value;
                    }
                }
            }

            return lReturnValue;
        }

        public long SoDuTKLuongTinh(string sMaSoTK, DateTime dt, int type)
        {
            long lReturnValue = 0;

            Dictionary<string, long> dictionary = new Dictionary<string, long>();
            var lstDoiTuong = (from p in GetAllCTTKKeToanLuongTinh()
                               join c in GetAllKhachHang() on p.DoiTuong equals c.MaSoKhachHang
                               join pv in GetAllNhaCungCap() on p.DoiTuong equals pv.Id.ToString()
                               where p.TaiKhoan.StartsWith(sMaSoTK)
                               select new { p1 = p, c1 = c, pv1 = pv });

            List<ButToan> lst = (from x in GetAllButToan()
                                 where x.NgayThang.Date <= dt.Date && x.TKGoc.StartsWith(sMaSoTK)
                                 select x).ToList();

            if (lst.Count > 0)
            {
                foreach (ButToan bt in lst)
                {
                    if (sMaSoTK.StartsWith("131"))
                    {
                        var c = lstDoiTuong.Where(o => o.c1.Ten == bt.MaSoKhachHang).FirstOrDefault();

                        long lTemp = 0;
                        if (c != null)
                        {
                            if (!dictionary.ContainsKey(c.p1.DoiTuong))
                            {
                                lTemp = (c.p1.SoDuBanDau + bt.GhiNo) - bt.GhiCo;
                                dictionary.Add(c.c1.Ten, lTemp);
                            }
                            else
                            {
                                lTemp = (dictionary[c.c1.Ten] + bt.GhiNo) - bt.GhiCo;
                                dictionary[c.c1.Ten] = lTemp;
                            }
                        }
                        else
                        {
                            if (!dictionary.ContainsKey(bt.MaSoKhachHang))
                            {
                                lTemp = bt.GhiNo - bt.GhiCo;
                                dictionary.Add(bt.MaSoKhachHang, lTemp);
                            }
                            else
                            {
                                lTemp = (dictionary[bt.MaSoKhachHang] + bt.GhiNo) - bt.GhiCo;
                                dictionary[bt.MaSoKhachHang] = lTemp;
                            }
                        }
                    }
                    else
                    {
                        var c = lstDoiTuong.Where(o => o.pv1.TenNhaCungCap == bt.MaSoKhachHang).FirstOrDefault();

                        long lTemp = 0;
                        if (c != null)
                        {
                            if (!dictionary.ContainsKey(c.p1.DoiTuong))
                            {
                                lTemp = (c.p1.SoDuBanDau + bt.GhiCo) - bt.GhiNo;
                                dictionary.Add(c.c1.Ten, lTemp);
                            }
                            else
                            {
                                lTemp = (dictionary[c.c1.Ten] + bt.GhiCo) - bt.GhiNo;
                                dictionary[c.c1.Ten] = lTemp;
                            }
                        }
                        else
                        {
                            if (!dictionary.ContainsKey(bt.MaSoKhachHang))
                            {
                                lTemp = bt.GhiCo - bt.GhiNo;
                                dictionary.Add(bt.MaSoKhachHang, lTemp);
                            }
                            else
                            {
                                lTemp = (dictionary[bt.MaSoKhachHang] + bt.GhiCo) - bt.GhiNo;
                                dictionary[bt.MaSoKhachHang] = lTemp;
                            }
                        }
                    }
                }

                foreach (var pair in dictionary)
                {
                    if (pair.Value >= 0 && type == 0)
                        lReturnValue += pair.Value;
                    else if (pair.Value < 0 && type == 1)
                        lReturnValue += 0 - pair.Value;
                }
            }

            return lReturnValue;
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<KhachHang> GetAllKhachHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = khachhangMgt.GetAllKhachHang();
            List<KhachHang> listKhachHang = (List<KhachHang>)resultObj.Result;

            return listKhachHang;
        }

        public List<NhaCungCap> GetAllNhaCungCap()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = nhacungcapMgt.GetAllNhaCungCap();
            List<NhaCungCap> listNhaCungCap = (List<NhaCungCap>)resultObj.Result;

            return listNhaCungCap;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();

            List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();

            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return lstTaiKhoanKeToan;
        }

        public List<BangCanDoiKeToan> GetAllBangCanDoiKeToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = bangcandoiketoanMgt.GetAllBangCanDoiKeToan();
            List<BangCanDoiKeToan> listBangCanDoiKeToan = (List<BangCanDoiKeToan>)resultObj.Result;

            return listBangCanDoiKeToan;
        }

        public List<CTTKKeToanLuongTinh> GetAllCTTKKeToanLuongTinh()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = cttkketoanluongtinhMgt.GetAllCTTKKeToanLuongTinh();
            List<CTTKKeToanLuongTinh> listCTTKKeToanLuongTinh = (List<CTTKKeToanLuongTinh>)resultObj.Result;

            return listCTTKKeToanLuongTinh;
        }

        public List<ChiTieuNgoaiBangCanDoiKeToan> GetAllChiTieuNgoaiBangCanDoiKeToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chitieungoaibangcandoiketoanMgt.GetAllChiTieuNgoaiBangCanDoiKeToan();
            List<ChiTieuNgoaiBangCanDoiKeToan> listChiTieuNgoaiBangCanDoiKeToan = (List<ChiTieuNgoaiBangCanDoiKeToan>)resultObj.Result;

            return listChiTieuNgoaiBangCanDoiKeToan;
        }
    }
}
